package com.fesine.ssh.service.impl;

import com.fesine.ssh.dao.CategoryDao;
import com.fesine.ssh.model.Category;
import com.fesine.ssh.service.CategoryService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by Fesine on 2016/2/21.
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    private static final Logger logger = Logger
            .getLogger(CategoryServiceImpl.class);

    @Autowired
    CategoryDao categoryDao;

    @Override
    public String addCategory(Category entity) {
        return categoryDao.save(entity);
    }

    @Override
    public String addChildCategory(int parent,Category entity) {
        String id=null;
        //根据父节点查询节点信息
        Category category = findByCateId(parent);
        if (null != category) {
            //如果父节点为叶子节点则更新父节点为非叶子节点
            if (category.TRUE_NUM == category.getLeaf()) {
                category.setLeaf(category.FALSE_NUM);
                categoryDao.update(category);//更新父节点
                logger.info("---->父类为叶子节点，更新父类为非叶子节点");
            }
            entity.setParent(parent);
            entity.setLeaf(category.TRUE_NUM);
            id = categoryDao.save(entity);//添加子节点
            logger.info("---->添加子分类成功，cateId:["+ entity.getCateId()+"]");
        }
        return id;
    }

    /**
     * 新增根节点,默认是叶子
     * @param entity
     * @return
     */
    @Override
    public String addTopCategory(Category entity) {
        entity.setParent(0);//根结点父节点都是0
        entity.setLeaf(entity.TRUE_NUM);//新增的节点是叶子
        return categoryDao.save(entity);
    }

    @Override
    public void modify(Category entity) {
        categoryDao.update(entity);
    }

    /**
     * 需要执行删除
     * @param id
     */
    @Override
    public void delete(String id) {
        categoryDao.delete(id);
    }


    /**
     * 递归删除
     * @param cateId
     */
    @Override
    public void deleteByCateId(int cateId) {
        // 类别删除完之后,需要判断当前父类是不是还有子类
        //如果没有子类,则需要将父类改为叶子节点
        Category category = this.findByCateId(cateId);
        int pid = category.getParent();
        //递归删除
        eachDelete(cateId);
        List<Category> listTemp = this.findByParent(pid);
        if (CollectionUtils.isEmpty(listTemp)) {
            //根据父id作为cateId查询类别信息
            Category cateParent = this.findByCateId(pid);
            cateParent.setLeaf(cateParent.TRUE_NUM);
            categoryDao.update(cateParent);
            logger.info("------->当前分类无子类，更新分类为叶子节点！");
        }
    }

    private void eachDelete(int cateId){
        //1 通过分类id查询所有子分类
        List<Category> list = this.findByParent(cateId);
        //2 存在子类
        if (list.size() > 0) {
            //3 递归删除
            for (Category cate : list) {
                logger.info("------->当前分类存在子分类，执行递归删除！分类Id:["+cate.getCateId()+"]");
                eachDelete(cate.getCateId());
            }
        }
        //删除自身
        Category category = this.findByCateId(cateId);
        categoryDao.delete(category.getId());

    }


    /**
     * 根据分类id查询分类信息,精确查找
     * @param cateId
     * @return
     */
    @Override
    public Category findByCateId(int cateId) {
        Category category = new Category();
        category.setCateId(cateId);
        List<Category> list  =categoryDao.findByEntity(category);
        if (!CollectionUtils.isEmpty(list)) {
            category = list.get(0);
        }else {
            category = null;
            logger.info("查找分类无数据，查询分类Id：["+cateId+"]");
        }
        return category;
    }

    @Override
    public List<Category> findByParent(int pid) {
        return categoryDao.findByParent(pid);
    }

    /**
     * 模糊查找
     * @param cateName
     * @return
     */
    @Override
    public List<Category> findByCateName(String cateName) {
        Category category = new Category();
        category.setText(cateName);
        return categoryDao.findByEntity(category);
    }

    @Override
    public List<Category> findAll() {
        return categoryDao.findAll();
    }

    @Override
    public List<Category> findTopAll() {
        return categoryDao.findTopAll();
    }

    @Override
    public List<Category> findByEntity(Category category) {
        return categoryDao.findByEntity(category);
    }
}
